package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityTaxCalculatorBinding;
import flc.ast.fragment.TaxKind1Fragment;
import flc.ast.fragment.TaxKind2Fragment;
import flc.ast.fragment.TaxKind3Fragment;
import stark.common.basic.event.EventStatProxy;

public class TaxCalculatorActivity extends BaseAc<ActivityTaxCalculatorBinding> {

    private Fragment mCurShowFragment;
    private Fragment taxKind1Fragment, taxKind2Fragment, taxKind3Fragment;

    @Override
    protected int onCreate() {
        return R.layout.activity_tax_calculator;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivTaxCalculateBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.tvTaxKind1.setOnClickListener(this);
        mDataBinding.tvTaxKind2.setOnClickListener(this);
        mDataBinding.tvTaxKind3.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvTaxKind1:
                clearSelection();
                showTaxKind1Fragment();
                mDataBinding.tvTaxKind1.setTextColor(Color.parseColor("#000000"));
                mDataBinding.tvTaxKind1.setTextSize(18);
                mDataBinding.ivTaxKind1.setVisibility(View.VISIBLE);
                break;
            case R.id.tvTaxKind2:
                clearSelection();
                showTaxKind2Fragment();
                mDataBinding.tvTaxKind2.setTextColor(Color.parseColor("#000000"));
                mDataBinding.tvTaxKind2.setTextSize(18);
                mDataBinding.ivTaxKind2.setVisibility(View.VISIBLE);
                break;
            case R.id.tvTaxKind3:
                clearSelection();
                showTaxKind3Fragment();
                mDataBinding.tvTaxKind3.setTextColor(Color.parseColor("#000000"));
                mDataBinding.tvTaxKind3.setTextSize(18);
                mDataBinding.ivTaxKind3.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected void initData() {
        showTaxKind1Fragment();
    }

    private void showTaxKind1Fragment() {
        if (taxKind1Fragment == null) {
            taxKind1Fragment = new TaxKind1Fragment();
        }
        showFragment(taxKind1Fragment);
    }

    private void showTaxKind2Fragment() {
        if (taxKind2Fragment == null) {
            taxKind2Fragment = new TaxKind2Fragment();
        }
        showFragment(taxKind2Fragment);
    }

    private void showTaxKind3Fragment() {
        if (taxKind3Fragment == null) {
            taxKind3Fragment = new TaxKind3Fragment();
        }
        showFragment(taxKind3Fragment);
    }

    private void showFragment(Fragment fragment) {
        if (fragment == null) return;
        if (fragment.isAdded() && fragment.isVisible()) return;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (mCurShowFragment != null) {
            transaction.hide(mCurShowFragment);
        }
        if (!fragment.isAdded()) {
            transaction.add(R.id.fl_container, fragment);
        } else {
            transaction.show(fragment);
        }
        mCurShowFragment = fragment;
        transaction.commitAllowingStateLoss();
    }

    private void clearSelection() {
        mDataBinding.tvTaxKind1.setTextColor(Color.parseColor("#A8A8A8"));
        mDataBinding.tvTaxKind1.setTextSize(15);
        mDataBinding.ivTaxKind1.setVisibility(View.GONE);
        mDataBinding.tvTaxKind2.setTextColor(Color.parseColor("#A8A8A8"));
        mDataBinding.tvTaxKind2.setTextSize(15);
        mDataBinding.ivTaxKind2.setVisibility(View.GONE);
        mDataBinding.tvTaxKind3.setTextColor(Color.parseColor("#A8A8A8"));
        mDataBinding.tvTaxKind3.setTextSize(15);
        mDataBinding.ivTaxKind3.setVisibility(View.GONE);
    }

}